package code

import (
	"bytes"
	"encoding/binary"
	"encoding/hex"
)

/**
将一个字节变两个字节，每四位从0123456789abcdef中取
*/
func Encode2Hex(src []byte) string {
	dst := make([]byte, len(src)*2)
	hex.Encode(dst, src)
	return string(dst)
}

/**
将形如0123456789abcdef每两个字节合成一个字节
*/
func DecodeHexStringToByte(src string) ([]byte, error) {
	dst := make([]byte, len(src)/2)
	_, err := hex.Decode(dst, []byte(src))
	if err != nil {
		return nil, err
	}
	return dst, nil
}

func IntToBytes(num uint64) []byte {
	var buffer bytes.Buffer
	binary.Write(&buffer, binary.BigEndian, num)
	return buffer.Bytes()
}

func BytesToInt(bs []byte) uint64 {
	buffer := bytes.NewBuffer(bs)
	var data uint64
	binary.Read(buffer, binary.BigEndian, &data)
	return data
}

func JoinBytes(bs ...[]byte) []byte  {
	//len:=len(bs)
	//fmt.Printf("%T",bs)//[][]uint8
	//bss := make([][]byte,len)
	//for i:=0;i<len;i++{
	//	bss[i] = bs[i]
	//}

	return bytes.Join(bs,[]byte{})

}
